from datetime import datetime, timedelta
import pytz

def convert_to_time(timestamp, timestamp_type):
    if timestamp_type == 1:
        # 秒数
        dt = datetime(1970, 1, 1, tzinfo=pytz.utc) + timedelta(seconds=timestamp)
    elif timestamp_type == 2:
        # 毫秒数
        dt = datetime(1970, 1, 1, tzinfo=pytz.utc) + timedelta(milliseconds=timestamp)
    else:
        raise ValueError("Invalid timestamp type. Use 1 for seconds or 2 for milliseconds.")

    # 获取本地时区
    local_tz = pytz.timezone('Asia/Shanghai')  # 替换为你的时区，例如 'Asia/Shanghai'
    local_datetime = dt.astimezone(local_tz)

    # 格式化为"yyyy-MM-dd HH:mm:ss"
    formatted_time = local_datetime.strftime('%Y-%m-%d %H:%M:%S')
    return formatted_time
    

# 示例用法
timestamp_in_seconds = 1754327503
timestamp_in_milliseconds = 1767146269000
print("秒数=" + convert_to_time(timestamp_in_seconds, 1))
print("毫秒数=" +convert_to_time(timestamp_in_milliseconds, 2))


# 定义时间字符串和格式
time_str = "2025-08-19 16:51:00"
time_format = "%Y-%m-%d %H:%M:%S"

# 将时间字符串转换为 datetime 对象
dt = datetime.strptime(time_str, time_format)

# 转换为 Unix 时间戳（秒数）
seconds = int(dt.timestamp())
print(f"秒数: {seconds}")

# 转换为 Unix 时间戳（毫秒数）
milliseconds = int(dt.timestamp() * 1000)
print(f"毫秒数: {milliseconds}")
